﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using System.Text;

namespace Catering.Catering_Page.Kufang
{
    public partial class cbydzs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            View_Monitor vm = new View_Monitor();
            int companyid = 0;
            int AreaID = Convert.ToInt32(this.DDLTool1.GetDDL_Area());
            int ClassID = Convert.ToInt32(this.DDLTool1.GetDDL_Class());
            int Year = 1949;
            if (!string.IsNullOrEmpty(this.DDLTool1.GetYear())) Year = Convert.ToInt32(this.DDLTool1.GetYear());
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
            }
            DataTable dt = vm.GetChengbenRenshuYueduZoushi(companyid, AreaID, ClassID, Year);
            Dictionary<string, Dictionary<int, decimal>> tempdic = InitDicModel();
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AreaID.ToString(), ClassID.ToString(), DateTime.Parse(Year + "-01-01 00:00:11"), "人均成本/就餐人数月度走势图");

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int tempmonth = Convert.ToInt32(dt.Rows[i]["MonthDay"]);
                    decimal tempnumber = Convert.ToDecimal(dt.Rows[i]["Number"]);
                    decimal tempmoney = 0;
                    if (!(dt.Rows[i]["AmountMoney"] is DBNull)) tempmoney = Convert.ToDecimal(dt.Rows[i]["AmountMoney"]);
                    if (tempdic.ContainsKey("人均成本") && tempdic["人均成本"].ContainsKey(tempmonth) && tempnumber > 0)
                    {
                        tempdic["人均成本"][tempmonth] = vm.GetMathResult(tempmoney / tempnumber, 0, 3);
                    }
                    if (tempdic.ContainsKey("实际就餐人数") && tempdic["实际就餐人数"].ContainsKey(tempmonth))
                    {
                        tempdic["实际就餐人数"][tempmonth] = tempnumber;
                    }
                }
                dt.Dispose();
            }

            string str = "";
            if (tempdic != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                int n = 0;
                foreach (string key in tempdic.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + key + "',yAxis: " + n + ",");
                    Sresult.Append("data: [");
                    string StrData = "";
                    for (int i = 0; i < 12; i++)
                    {
                        if (tempdic[key].ContainsKey(i + 1)) StrData += tempdic[key][i + 1] + ",";
                        else StrData += "0,";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    Sresult.Append("},");
                    n++;
                }
                str = Sresult.ToString();
            }
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + str + ");", true);
        }

        private Dictionary<string, Dictionary<int, decimal>> InitDicModel()
        {
            Dictionary<string, Dictionary<int, decimal>> tempdic = new Dictionary<string, Dictionary<int, decimal>>();
            tempdic.Add("人均成本", InitSingleModel());
            tempdic.Add("实际就餐人数", InitSingleModel());
            return tempdic;
        }

        private Dictionary<int, decimal> InitSingleModel()
        {
            Dictionary<int, decimal> temp1 = new Dictionary<int, decimal>();
            for (int i = 1; i < 13; i++)
            {
                temp1.Add(i, 0);
            }
            return temp1;
        }
    }
}